package com.dinglexiao.controller;

import com.dinglexiao.common.lang.Result;
import com.dinglexiao.dto.AccountDTO;
import com.dinglexiao.entity.User;
import com.dinglexiao.service.UserService;
import com.dinglexiao.utils.JwtUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author dingl
 * @ClassName : AccountController
 * @Description : 账户接口，登录，注册
 * @date 2020/9/18-9:39
 */
@RestController
public class AccountController {

    @Autowired
    UserService userService;

    @Autowired
    JwtUtils jwtUtils;

    /***
    * @description:用户登录
    * @param accountDTO
	 * @param response
    * @return com.dinglexiao.common.lang.Result
    * @author dlx
    * @date 2020/9/18-11:19
    */
    @PostMapping("/login")
    public Result login(@Validated @RequestBody AccountDTO accountDTO, HttpServletResponse response){
        Result result = userService.validateLogin(accountDTO,response);
        return result;
    }

    /***
    * @description:用户注册
    * @param user
    * @return com.dinglexiao.common.lang.Result
    * @author dlx
    * @date 2020/9/18-11:20
    */
    @PostMapping("/register")
    public Result saveAccount(@Validated @RequestBody User user){
        return userService.registerAccount(user);
    }

    /***
    * @description:退出
    * @param
    * @return com.dinglexiao.common.lang.Result
    * @author dlx
    * @date 2020/9/18-16:14
    */
    @RequiresAuthentication
    @GetMapping("/logout")
    public Result logOut(){
        SecurityUtils.getSubject().logout();
        return Result.success(null);
    }
}
